package com.example.springbootstartermqttprovider.quert;



import java.util.List;

public class QrUrlParser {

    public static String getDidiBikeNo(String qrUrl) {
        List<String> blob = RegExpUtils.grep(qrUrl, "=(.*)&");
        if (blob.size() == 0) {
            return "";
        }
        int size = blob.get(0).length();
        return blob.get(0).substring(1, size-1);
    }

    public static String getDidiEBikeNo(String qrUrl) {
        List<String> blob = RegExpUtils.grep(qrUrl, "[0-9]*$");
        if (blob.size() == 0) {
            return "";
        }
        return blob.get(0);
    }

    public static String getHelloBikeNo(String qrUrl) {
        List<String> blob = RegExpUtils.grep(qrUrl, "[0-9]{10}");
        if (blob.size() == 0) {
            return "";
        }

        return blob.get(0);
    }

    public static String getMeiTuanBikeNo(String qrUrl) {
        List<String> blob = RegExpUtils.grep(qrUrl, "=(.*)_");
        if (blob.size() == 0) {
            return "";
        }
        int size = blob.get(0).length();
        return blob.get(0).substring(1, size-1);
    }

    public static String getXmBikeNo(String qrUrl) {
        List<String> blob = RegExpUtils.grep(qrUrl, "[0-9]*$");
        if (blob.size() == 0) {
            return "";
        }
        return blob.get(0);
    }



    public static Integer getXiaoLiuBikeType(String bikeNo) {
        if(RegExpUtils.validateBikeType(bikeNo, "57[0-9]{7}") || RegExpUtils.validateBikeType(bikeNo, "27[0-9]{7}")){
            return 2;
        } else if (RegExpUtils.validateBikeType(bikeNo, "17[0-9]{7}") ) {
            return 1;
        }else {
            return 3;
        }
    }

    public static String getSongGuoBikeNo(String qrUrl) {
        List<String> blob = RegExpUtils.grep(qrUrl, "[0-9]*$");
        if (blob.size() == 0) {
            return "";
        }

        return blob.get(0);
    }

    public static String getGenBikeNo(String qrUrl) {
        List<String> blob = RegExpUtils.grep(qrUrl, "[0-9]*$");
        if (blob.size() == 0) {
            return "";
        }

        return blob.get(0);
    }

    public static String getQingChengChiTuBikeNo(String qrUrl) {
        List<String> blob = RegExpUtils.grepTwo(qrUrl, "code=(\\d+)");
        if (blob.size() == 0) {
            return "";
        }

        return blob.get(0);
    }

    /**
     *
     * @param bikeNo
     * @return
     *
     *  * E Bike
     *  * MTeC1.0 866 020 5001 - 866 030 5000
     *  * MTeC1.1 866 032 9001 - 866 037 9000
     *  * MTeS5.0 866 100 0000 - 866 119 9999
     *  * MTeS5.1 866 120 0000 - 866 139 9999
     *  * MTeS5.2 866 140 0000 - 866 169 9999
     *  * MTeS8.0 866 200 0000 - 866 299 9999
     *  * MTeS2.0 866 300 0000 - 866 399 9999
     */
    public static boolean isMeiTuanEBike(String bikeNo) {
        if (bikeNo.length() < 10) {
            return false;
        }

        if (!"866".equals(bikeNo.substring(0, 3))) {
            return false;
        }

        long number = Long.parseLong(bikeNo.substring(3));
        if (number >= 205001 && number <= 305000) {
            return true;
        } else if (number >= 329001 && number <= 379000) {
            return true;
        } else if (number >= 1000000 && number <= 1199999) {
            return true;
        } else if (number >= 1200000 && number <= 1399999) {
            return true;
        } else if (number >= 1400000 && number <= 1699999) {
            return true;
        } else if (number >= 2000000 && number <= 2999999) {
            return true;
        } else if (number >= 3000000 && number <= 3999999) {
            return true;
        } else if (number >= 4000000 && number <= 8999999) {
            return true;
        }

        return false;
    }


    public static void main(String[] args) {
        // 青桔单车
        String qjUrl = "https://dc.tt/htw?id=18D56A1&p=2";
        System.out.println(QrUrlParser.getDidiBikeNo(qjUrl));
        System.out.println(Integer.parseInt("18D56A1", 16));

        // 青桔电单车
        String qjeUrl = "https://dc.tt/hm?vehicleId=22819721";
        System.out.println(QrUrlParser.getDidiEBikeNo(qjeUrl));

        // 哈啰
        String hbUrl = "http://c3x.me/?n=3110463460";
        System.out.printf("hb:%s\n", QrUrlParser.getHelloBikeNo(hbUrl));
        hbUrl = "http://c3x.me/?x=5800003648";
        System.out.println(QrUrlParser.getHelloBikeNo(hbUrl));


        String mtUrl = "http://www.mobike.com/download/app.html?b=8661703340_1";
        System.out.println(QrUrlParser.getMeiTuanBikeNo(mtUrl));
        mtUrl = "http://www.mobike.com/download/app.html?b=E232061732_1";
        System.out.println(QrUrlParser.getMeiTuanBikeNo(mtUrl));

        String bikeNo = "8663999999";
        System.out.println(bikeNo.substring(0, 3));
        System.out.println(bikeNo.substring(3));
        System.out.println("tail long:"+Long.parseLong(bikeNo.substring(3)));

        String xmUrl = "https://wx.huahuabike.com/?machineNO=03592296";
        System.out.println(QrUrlParser.getXmBikeNo(xmUrl));

        String songGuoUrl = "https://m.songguo7.com/index?bikesn=815752960";
        System.out.println(QrUrlParser.getSongGuoBikeNo(songGuoUrl));

        String xiaoliuUrl = "http://app.xlgxapp.com/download?bno=574336014";
        System.out.println(QrUrlParser.getGenBikeNo(xiaoliuUrl));
        System.out.println(QrUrlParser.getXiaoLiuBikeType("574336014"));
    }
}
